﻿<?PHP
class SimpleResultTable
{
	private $html = null;
	
	private $resultTableNameStr = null;
	private $resultQueryArray = null;
	private $columnHeaderArray = null;

	public function __construct($resultTableNameStr,$columnHeaderArray,$resultQueryArray){
		$this->resultTableNameStr = $resultTableNameStr;
		$this->resultQueryArray = $resultQueryArray;
		$this->columnHeaderArray = $columnHeaderArray;
	}

	public function getResultTableNameStr(){
		return $this->resultTableNameStr;
	}

	public function setResultTableNameStr($resultTableNameStr){
		$this->resultTableNameStr = $resultTableNameStr;
	}

	public function getResultQueryArray(){
		return $this->resultQueryArray;
	}

	public function setResultQueryArray($resultQueryArray){
		$this->resultQueryArray = $resultQueryArray;
	}

	public function getColumnHeaderArray(){
		return $this->columnHeaderArray;
	}

	public function setColumnHeaderArray($columnHeaderArray){
		$this->columnHeaderArray = $columnHeaderArray;
	}
	
	/**
	*  Retorna o código html necessário para renderizar uma tabela populada a partir do conteudo do objeto Query.
	**/
	public function getHTML(){
	
		$srt_id = $this->resultTableNameStr.'_id_check';
	
		$this->html = '';
		$this->html = $this->html.'<div id="num_rows">';
		$this->html = $this->html.$this->resultQueryArray->num_rows;
		$this->html = $this->html.'</div>';
		
		$this->html = $this->html.'<div id="resultTable">';
		$this->html = $this->html.'<table id="table_'.$this->resultTableNameStr.'" class="srt_table">';
		$this->html = $this->html.'<tr class="srt_tr_alias">';
		
		$this->html = $this->html.'<td class="check_collun"></td>';
		
		foreach ($this->columnHeaderArray as $i => $value) {
			
			if($this->columnHeaderArray[$i] != null){
				$this->html = $this->html.'<td class="srt_td_alias">';
				$this->html = $this->html.$this->columnHeaderArray[$i];
				$this->html = $this->html.'</td>';
			}
		}
		
		$this->html = $this->html.'</tr>';			
		
		if ($this->resultQueryArray != null) {
		$zebrado = 0;
			while ($row = $this->resultQueryArray->fetch_row()) {
				$this->html = $this->html.'<tr class="srt_tr_result_'.$zebrado.'">';
				
				$this->html = $this->html.'<td class="check_collun_value" width="10px">';
				$this->html = $this->html.'<input type="checkbox" id="'.$srt_id.'" name="'.$srt_id.'" value="'.$row[0].'" />';
				$this->html = $this->html.'</td">';
				
				foreach ($this->columnHeaderArray as $i => $value) {
					if($this->columnHeaderArray[$i] != null){
						$this->html = $this->html.'<td class="srt_td_result_'.$zebrado.'">';
						if($i <= sizeof($row)-1){
						  $this->html = $this->html.$row[$i];
						}else{
						  // usuário adicionou mais alias do que podia
						  $this->html = $this->html.'<center>?</center>';
						}
						$this->html = $this->html.'</td>';
					}
				}
				
				$this->html = $this->html.'</tr>';
				if($zebrado == 0){
					$zebrado = 1;
				}else{
					$zebrado = 0;
				}
			}
			$this->resultQueryArray->close();
		}
		$this->html = $this->html.'</table>';
		
		$this->html = $this->html.'<input type="hidden" id="'.$srt_id.'_hidden" name="'.$srt_id.'_hidden" />';
		
		$this->html = $this->html.'<script>addListenerCheckboxSimpleResultTable("'.$srt_id.'")</script>';
		
		$this->html = $this->html.'</div>';
		
		return $this->html;
	}
	
	public function show(){
		echo $this->getHTML();
	}
	
}
?>